<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 



/**
* This function sends an email to a person using a template and data to fill it.
* Hack: If no data is received, the template will be used as html for the content.
* Returns true on success and false if it can't send the email.
* 
* @param from_email 
* @param from_name
* @param to_email
* @param subject
* @param template
* @param template_data
* @return boolean
* 
*/

if ( ! function_exists('send_email'))
{  	

  	function send_email($from_email, $from_name, $to_email, $subject, $template, $template_data=NULL)
  	{	
		$CI =& get_instance();	
	
		if ($template_data!==NULL) {
			$fd = fopen ($template, "r");
			$mailcontent = fread ($fd, filesize ($template));
			foreach ($template_data as $key=>$value)
			{
				$mailcontent = str_replace("%%".$key."%%", $value,$mailcontent );
			}		
			fclose ($fd);	

		} else {
			$mailcontent = $template;
		}
		
		$mailcontent = stripslashes($mailcontent);
		
		// email information
		
		$CI->load->library('email');

        $CI->email->set_newline("\r\n"); 
		$CI->email->crlf = "\n";
		
		$CI->email->from($from_email, $from_name);
		$CI->email->to($to_email);
		$CI->email->subject($subject);
        
		$CI->email->message($mailcontent);
		// sending the email.
       
		if(!$CI->email->send()){
            echo $CI->email->print_debugger();
            $CI->email->clear();
			return FALSE;
		}else{
			return TRUE;
        }
	}
}
?>
